package com.pku.control;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.ArrayList;
import java.util.List;

public class RecyclerViewApater extends RecyclerView.Adapter<RecyclerViewApater.BaseViewHolder>{
    private List<DeviceInfo> dataList;
    public  Context context;
    public RecyclerViewApater(List<DeviceInfo> dataList, Context context) {
        this.dataList = dataList;
        this.context=context;
    }

    public void replaceAll(ArrayList<DeviceInfo> list) {
        dataList.clear();
        if (list != null && list.size() > 0) {
            dataList.addAll(list);
        }
        notifyDataSetChanged();
    }

    /**
     * 插入数据使用notifyItemInserted，如果要使用插入动画，必须使用notifyItemInserted
     * 才会有效果。即便不需要使用插入动画，也建议使用notifyItemInserted方式添加数据，
     * 不然容易出现闪动和间距错乱的问题
     * */
    public void addData(int position,ArrayList<DeviceInfo> list) {
        dataList.addAll(position,list);
        notifyItemInserted(position);
    }

    //移除数据使用notifyItemRemoved
    public void removeData(int position) {
        dataList.remove(position);
        notifyItemRemoved(position);
    }


    @Override
    public RecyclerViewApater.BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OneViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerViewApater.BaseViewHolder holder, int position) {
        holder.setData(dataList.get(position),position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context,ControlActivity.class);
                intent.putExtra("maccode",dataList.get(position).getMac_code());
                context.startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return dataList != null ? dataList.size() : 0;
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(View itemView) {
            super(itemView);
        }

        void setData(Object data,int position) {

        }
    }

    private class OneViewHolder extends BaseViewHolder {
        private ImageView iv;
        private TextView tv;
        public OneViewHolder(View view) {
            super(view);
            iv=view.findViewById(R.id.iv);
            tv=view.findViewById(R.id.tv_name);
        }

        @Override
        void setData(Object data,int position) {
            if (data != null) {
                DeviceInfo deviceInfo=(DeviceInfo)data;
                int id =  deviceInfo.getDrawId();
                tv.setText(deviceInfo.getMachine_name());
                iv.setImageResource(id);
                //需要Item高度不同才能出现瀑布流的效果，此处简单粗暴地设置一下高度
                if (position % 2 == 0) {
                    iv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, UIUtil.dip2px(context,200)));
                } else {
                    iv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, UIUtil.dip2px(context,160)));
                }
            }
        }
    }
}
